import dayjs from "dayjs";
import { onUnmounted, ref, computed } from "vue";
export const useCountDown = () => {
  let timer = null;
  // 响应式数据
  const time = ref(0);
  // 格式化时间
  const formatTime = computed(() => {
    return dayjs.unix(time.value).format("mm分ss秒");
  });
  const start = (currentTime) => {
    //开启倒计时的逻辑‘
    // 核心逻辑的编写，每隔一秒就减1
    time.value = currentTime;
    timer = setInterval(() => {
      time.value--;
    }, 1000);
  };
  //   组件销毁时清除定时器
  onUnmounted(() => {
    timer && clearInterval(timer);
  });
  return {
    formatTime,
    start,
  };
};
